
// ensure that correct version is send to the client regardless to caching
// version: 3

//async script, twitter button fashiolista.com style
(function() {
    var s = document.createElement('script');
    var c = document.getElementsByTagName('script')[0];
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'http://platform.twitter.com/widgets.js';
    c.parentNode.insertBefore(s, c);
})();

$(document).ready( function () {
    // pretty tips in inputs
    $(".search-text").each( function () {
        $(this).prop("value", objectL10n.search);
    });
    $(".search-text").each( function () {
        $(this).focus( function () {
            if ($(this).val() == objectL10n.search)
                $(this).prop("value", "");
        });
    });
    $(".search-text").each( function () {
        $(this).blur( function () {
            if ($(this).val() == "")
                $(this).prop("value", objectL10n.search);
        });
    });

    // icons near links in blogroll, enetri.com style
    $("ul.blogroll li a").each(function(){
        var externalURL = $(this).attr("href");
        var faviconURL = externalURL.replace(/^(http|https)(:\/\/[^\/]+).*$/, '$1$2') + '/favicon.ico';
        var img = $('<img src="' + template_dir + 'images/external.png" alt="">').insertBefore(this);
        $(img).wrap('<a href="' + externalURL + '" onclick="window.open(this.href);return false;" style="margin-right:4px"></a>');
        var ico = new Image();
        ico.src = faviconURL;
        if (ico.complete) img.attr('src', faviconURL);
        else ico.onload = function(){ img.attr('src', faviconURL) };
    });

    // validation for comments input
    $("#commentform").submit( function () {
        // check for empty required fields
        if ($("input#author").val() == "" || $("input#email").val() == "" || $("textarea#comment").val() == "") {
            alert(objectL10n.empty_fields);
            return false;
        }
        // primal check for valid email
        var x = $("input#email").val();
        var atpos = x.indexOf("@");
        var dotpos = x.lastIndexOf(".");
        if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
            alert(objectL10n.valid_email);
            return false;
        }
    });
});

